<?php
$vAllTrees = AFT_MakeTreeDB('all');	
$vSettings = array();
$vSettings = AFT_load('settings');
$vshowall='';
if ($vSettings['showalltrees']==true) $vshowall=' checked';
global $icons;
?>

<center>
<form name="cb">
<b>Sort by</b>
<select name="sort" size="1" onchange="fAction('faction=actionsort&actn=',this.value)" >
	<option value="realname" <?php if ($vSettings['sort']=='realname') echo ' selected';?>>Name</option>
	<option value="mastery" <?php if ($vSettings['sort']=='mastery') echo ' selected';?>>Mastery</option>
	<option value="coins" <?php if ($vSettings['sort']=='coins') echo ' selected';?>>Coins</option>
	<option value="cocos" <?php if ($vSettings['sort']=='cocos') echo ' selected';?>>Coconuts</option>
	<option value="date" <?php if ($vSettings['sort']=='date') echo ' selected';?>>Date</option>
	<option value="cnt" <?php if ($vSettings['sort']=='cnt') echo ' selected';?>>Count</option>
	<option value="cntSeed" <?php if ($vSettings['sort']=='cntSeed') echo ' selected';?>>Seed</option>
	<option value="cntGB" <?php if ($vSettings['sort']=='cntGB') echo ' selected';?>>InGB</option>
	<option value="cntST" <?php if ($vSettings['sort']=='cntST') echo ' selected';?>>InST</option>
	<option value="cntOrc" <?php if ($vSettings['sort']=='cntOrc') echo ' selected';?>>InOrc</option>
</select>
<select name="orderby" size="1" onchange="fAction('faction=actionorderby&actn=',this.value)" >
	<option value="asc" <?php if ($vSettings['orderby']=='asc') echo ' selected';?>>Asc</option>
	<option value="desc" <?php if ($vSettings['orderby']=='desc') echo ' selected';?>>Desc</option>
</select>
<input type="button" value="Sort" onClick="history.go(0)"/>
&nbsp;
<input type="checkbox" name="chkbx" value="ok"<?php echo $vshowall; ?> onclick="fAction('faction=actionshowall&showalltrees=','cbc')">
Show All Trees (<b>not</b> recommended)?&nbsp;
<input type="checkbox" class="saveConf" name="settings-hideMastered" <?php echo $vSettings['hideMastered']?"checked=\"checked\"":""; ?> >
Hide mastered trees?</form>
 
 
<table class="grid sortable" id="trees">
	<caption>Trees on the farm</caption>
	</caption>
	<thead>
		<tr>
			<th>Image</th>
			<th width="200">Name</th>
			<th>Mastery</th>
			<th>Release</th>
			<th>Coins</th>
			<th>Cocos</th>
			<th>Jade</th>
			<th>Count</th>
			<th>Orchard</th>
			<th>Trees</th>
			<th>Seedlings</th>
			<th>Giftbox</th>
			<th>Storage</th>
			<th>Breeding</th>
			<th>Move To</th>
<?php
$vUnlockedWorldtypes=load_botarray(F('unlockedworldtypes.txt'));
$vUnlockedLicences=load_botarray(F('unlockedworldlicenses.txt'));
$vSettingsWorldtype=@file_get_contents(F('worldtype.txt'));
$vworlds=array();
foreach ($vUnlockedLicences as $vUn)
	if ($vUn !== $vSettingsWorldtype) $vworlds[]=$vUn;
	if (in_array($vSettingsWorldtype, $vUnlockedLicences) && count($vworlds)>0)
	echo '<th>Transport</th>';
?>						
		</tr>
	</thead>
	<tbody>
<?php

    $vSections = array();
    $vSections = AFT_load('sections');
	$objects  = @unserialize(file_get_contents(F('objects.txt')));
	$vOrchards = array();
	foreach ($objects as $oID => $o) {
		if ($o['className']=='OrchardFeatureBuilding'  || $o['className']=='OrchardBuilding' || $o['className'] == "ArboretumBuilding") {	
			$num = 0;
			foreach ($o['contents'] as $trees) {
				$num = $num + $trees['numItem'];
			}
			$o['itemscnt']=$num;
			$vOrchards[] = $o;
		}
	}

	usort($vOrchards, function ($a, $b) {
		return $a['id'] - $b['id'];
	});
	
	
	$vtcountall=0;
	$vtcountun=0;
	$vtcountseed=0;
	$vtcountgb=0;
	$vtcountst=0;
	$vtcountorc=0;

	if (strlen($vSettings['sort'])==0) $vSettings['sort']='realname';

	foreach ($vAllTrees as $vATr) {
		$vatmp[]=$vATr[$vSettings['sort']];
	}

    $vSections = array();
    $vSections = AFT_load('sections');

	if ($vSettings['orderby']=='desc') {
		array_multisort($vatmp, SORT_DESC, $vAllTrees); 
	} else array_multisort($vatmp, SORT_ASC, $vAllTrees); 

	foreach ($vAllTrees as $vATr) {
		if ($vSettings['hideMastered'] && $vATr['mastery'] >= 1) continue;
		if (!$vATr['code']) continue;
		if ($vSettings['showalltrees']==true || $vATr['cnt']>0 || $vATr['cntOrc'] > 0 ) {
			$class = $vATr['mastery'] >= 1?"mastered":"";
			echo "<tr class=\"$class\" tree=\"{$vATr['code']}\">";
			if ($vSettings['interface'] == "simple") {
				echo AFT_tr($vATr,array('icon','name','mastery','count'));
			} else {
				echo AFT_tr($vATr,array('icon','name','mastery','release','coins','cocos','jade','count','countOrchard'));
			}

// SELL ?>
		<td>
			<img class="icon<?=$vATr['action']=="sell"?" on":""?>" src="<?=$icons['sell'.($vATr['action']=="sell"?"":"Off")]?>" title="sell" action="action">
			<br>
			Keep:&nbsp;<input maxlength="6" name="keep" size="3" type="text" value="<?=$vATr['actioncnt']?>"  
						onchange="fAction('faction=actioncnt&code=<?=urlencode($vATr['code'])?>&val=',this.value)" >
		</td>
<?php // SEEDS			 ?>		
		<td class="colorme" count="<?=$vATr['cntSeed']?>">
			<img class="icon<?=$vATr['actionSeed']=="water"?" on":""?>" src="<?=$icons['water'.($vATr['actionSeed']=="water"?"":"Off")]?>" title="water" action="actionSeed">
			<img class="icon<?=$vATr['actionSeed']=="sell"?" on":""?>" src="<?=$icons['sell'.($vATr['actionSeed']=="sell"?"":"Off")]?>" title="sell" action="actionSeed">
			<br>
			Count:&nbsp;<input maxlength="6" name="keep" size="3" type="text" value="<?=$vATr['actionSeedcnt']?>"  
							onchange="fAction('faction=actionSeedcnt&code=<?=urlencode($vATr['code'])?>&val=',this.value)" >/<?=$vATr['cntSeed']?>
		</td>

<?php // GIFTBOX ?>
		<td class="colorme" count="<?=$vATr['cntGB']?>">
			<img class="icon<?=$vATr['actionGB']=="sell"?" on":""?>" src="<?=$icons['sell'.($vATr['actionGB']=="sell"?"":"Off")]?>" title="sell" action="actionGB">
			<img class="icon<?=$vATr['actionGB']=="place"?" on":""?>" src="<?=$icons['place'.($vATr['actionGB']=="place"?"":"Off")]?>" title="place" action="actionGB">
			<br>
			Keep:&nbsp;<input maxlength="6" name="keep" size="3" type="text" value="<?=$vATr['actionGBcnt']?>"  
						onchange="fAction('faction=actionGBcnt&code=<?=urlencode($vATr['code'])?>&val=',this.value)" >/<?=$vATr['cntGB']?>
		</td>

<?php 		// STORAGE  ?>
		<td class="colorme" count="<?=$vATr['cntST']?>">
			<img class="icon<?=$vATr['actionST']=="store"?" on":""?>" src="<?=$icons['store'.($vATr['actionST']=="store"?"":"Off")]?>" title="store" action="actionST">
			<img class="icon<?=$vATr['actionST']=="sell"?" on":""?>" src="<?=$icons['sell'.($vATr['actionST']=="sell"?"":"Off")]?>" title="sell" action="actionST">
			<img class="icon<?=$vATr['actionST']=="place"?" on":""?>" src="<?=$icons['place'.($vATr['actionST']=="place"?"":"Off")]?>" title="place" action="actionST">
			<br>
			Keep:&nbsp;<input maxlength="6" name="keep" size="3" type="text" value="<?=$vATr['actionSTcnt']?>"  
						onchange="fAction('faction=actionSTcnt&code=<?=urlencode($vATr['code'])?>&val=',this.value)" >/<?=$vATr['cntST']?>
		</td>

<?php // Breeding ?>
		<td>
			<select name="actn" size="1" style="font-size:11px;width:70px;" onchange="fAction('faction=actionBred&code=<?=urlencode($vATr['code'])?>&val=',this.value)" >
<?php
foreach (array(
	array('',''),
	array('forced','Forced'),
	array('tomast','To Mastery'),
	array('tocnt','To Count'),
	array('spare','Spare')
	)	as $m) {
	echo "<option value=\"{$m[0]}\"".($vATr['actionBred']==$m[0]?" selected":"").">{$m[1]}</option>\n";
}
?>
			</select><br>
			Count:&nbsp;<input maxlength="6" name="keep" size="3" type="text" value="<?=$vATr['actionBredcnt']?>"  onchange="fAction('faction=actionBredcnt&code=<?=urlencode($vATr['code'])?>&val=',this.value)" >
		</td>
<?php // MOVE ?>
		<td>
			Sec:&nbsp;<select name="actn" size="1" style="font-size:11px;width:80px;" onchange="fAction('faction=movetosec&code=<?=urlencode($vATr['code'])?>&val=',this.value)" >
						<option value="">&nbsp;</option>
<?php
foreach ($vSections as $vsec) {
	echo "<option value=\"{$vsec['id']}\" ".($vATr['movetosec']==$vsec['id']?" selected":"").">".($vsec['name'] ?"{$vsec['name']} ({$vsec['id']})": $vsec['id']).'</option>';
}
?>
			</select><br>
			Orc:&nbsp;<select name="actn" size="1" style="font-size:11px;width:80px;" onchange="fAction('faction=movetoorc&code=<?=urlencode($vATr['code'])?>&val=',this.value)" >
				<option value="">&nbsp;</option>
<?php
foreach ($vOrchards as $vsec) {
	$capacity = 20 + (($vsec['expansionLevel']-1) * 10); //20;
//	if (stristr($vsec['itemName'] ,"superorchard")) $capacity = 40;
	echo '<option value="'.$vsec['id'].'"'.($vATr['movetoorc']==$vsec['id']?" selected":"").'>'.substr($vsec['className'],0,1).'-'.$vsec['id'].' ['.$vsec['itemscnt'].'/'.$capacity.']</option>';
}
?>
			</select>
		</td>
		<td>
<?php
//transport
if (in_array($vSettingsWorldtype, $vUnlockedLicences) && count($vworlds)>0) {
?>
	<select name="actn" size="1" style="font-size:11px;width:80px;" onchange="fAction('faction=actiontransp&code=<?=urlencode($vATr['code'])?>&val=',this.value)" >
<?php
	$vslct='';
	if ($vATr['actiontransp']=='0') $vslct=' selected';
	echo '<option value="0"'.$vslct.'>&nbsp;</option>';
	foreach ($vworlds as $vworld) {
		$vslct='';
		if ($vATr['actiontransp']==$vworld) $vslct=' selected';
		echo '<option value="'.$vworld.'"'.$vslct.'>'.$vworld.'</option>';
	}
?>
	</select><br>
	Keep:&nbsp;<input maxlength="6" name="keep" size="3" type="text" value="<?=intval($vATr['actiontranspcnt'])?>"  
onchange="fAction('faction=actiontranspcnt&code=<?=urlencode($vATr['code'])?>&val=',this.value)" ></td>
<?php
	echo '</td>';
}

		echo "</tr>\n\n"; 
		$vtcountall += $vATr['cnt'];
		if ($vATr['mastery'] < 1) {
			$vttcountun ++;
			$vtcountun += $vATr['cnt'];
			$masneeded += $vATr['masmax'] - $vATr['mascnt'];
		} else {
			$vttcount ++;
		}
		$vtcountseed += $vATr['cntSeed'];
		$vtcountgb  += $vATr['cntGB'];
		$vtcountst += $vATr['cntST'];
		$vtcountorc += $vATr['cntOrc'];
	}
}

//all trees
?>
		</tbody>
		<tfoot>
			<tr>
				<th title="mastered/un-mastered tree types"><?=$vttcount?>/<?=$vttcountun?></th>
				<th>&nbsp;<b>All Trees</b></th>
				<th title="mastery points needed"><?=$masneeded?></th>
				<th>&nbsp;</th>
				<th>&nbsp;</th>
				<th>&nbsp;</th>
				<th>&nbsp;</th>
				<th title="unmastered/all trees"><?=$vtcountun?>/<?=$vtcountall?></th>
				<th title="in Orchards"><?=$vtcountorc?></th>
				<th>&nbsp;</th>
				<th title="Seeds"><?=$vtcountseed?></th>
				<th title="in Giftbox"><?=$vtcountgb?></th>
				<th title="in Storage"><?=$vtcountst?></th>
				<th>&nbsp;</th>
				<th>&nbsp;</th>
				
<?php if (in_array($vSettingsWorldtype, $vUnlockedLicences) && count($vworlds)>0)
	echo '<th>&nbsp;</th>';
?>
				
			</tr>
		</tfoot>
	</table>
</center>
